package com.cssl.service.impl;


import com.cssl.domain.DtsAd;
import com.cssl.domain.dto.DtsAdDto;
import com.cssl.mapper.DtsAdMapper;

import com.cssl.service.DtsAdService;
import com.cssl.util.RedisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 黄鑫
 * @description 针对表【dts_ad(广告表)】的数据库操作Service实现
 * @createDate 2024-08-06 11:53:10
 */
@Service
public class DtsAdServiceImpl
        implements DtsAdService {
    @Autowired
    private DtsAdMapper dtsAdMapper;
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public Map getDtsAdList(DtsAdDto dto) {

        // Redis Key 存储，包含页码和条件
        StringBuilder redisKeyBuilder = new StringBuilder("DtsAdList");

        // 检查手机号
        if (dto.getContent() != null && !dto.getContent().isEmpty()) {
            redisKeyBuilder.append(":Content:").append(dto.getContent());
        }
        // 检查昵称
        if (dto.getName() != null && !dto.getName().isEmpty()) {
            redisKeyBuilder.append(":getName:").append(dto.getName());
        }


        redisKeyBuilder.append(":page:").append(dto.getPageNum());
        String redisKey = redisKeyBuilder.toString();
        List<DtsAd> adList = null;
        // 检查 Redis 中是否存在当前页的数据
        if (redisUtil.hasKey(redisKey)) {

            adList = (List<DtsAd>) redisUtil.get(redisKey);
            Map<String, Object> map1 = new HashMap<>();
            // 获取存储的总数

            map1.put("list", adList);
            map1.put("total", redisUtil.get("DtsAdList:total")); // 使用从 Redis 获取的总数
            map1.put("pageNum", dto.getPageNum());
            map1.put("pageSize", dto.getPageSize());
            return map1;
        } else {
            // 开始分页
            Page page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

            adList = dtsAdMapper.getDtsAdList(dto);
            Map<String, Object> map = new HashMap<>();
            // 计算总数
//            long total = page.getTotal();

            // 将查询结果放入 Redis，设置过期时间，例如：1小时
            redisUtil.set(redisKey, adList, 3600);
            redisUtil.set("DtsAdList:total", dtsAdMapper.getDtsAdCount(), 3600);

            map.put("list", page.getResult());
            map.put("total", page.getTotal()); // 使用从数据库计算的总数
            map.put("totalPage", page.getPages());
            map.put("pageNum", page.getPageNum());
            map.put("pageSize", page.getPageSize());
            return map;
        }
    }

    @Override
    public int createAd(DtsAd dtsAd) {

        // 首先检查是否有任何以 "DtsAdList:" 开头的键
        Set<String> keys = redisUtil.keys("DtsAdList:*");
        if (!keys.isEmpty()) {
            // 删除所有匹配的键
            for (String key : keys) {
                redisUtil.del(key);
            }
        }


        return dtsAdMapper.createAd(dtsAd);
    }
}




